package org.mcclone.cas.authentication;

import org.apereo.cas.authentication.HandlerResult;
import org.apereo.cas.authentication.UsernamePasswordCredential;
import org.apereo.cas.authentication.handler.support.AbstractUsernamePasswordAuthenticationHandler;
import org.apereo.cas.authentication.principal.Principal;
import org.apereo.cas.authentication.principal.PrincipalFactory;
import org.apereo.cas.services.ServicesManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

/**
 * 自定义登录认证处理器
 *
 * @author McClone
 */
public class CustomAuthenticationHandler extends AbstractUsernamePasswordAuthenticationHandler {
    private static final Logger logger = LoggerFactory.getLogger(CustomAuthenticationHandler.class);

    public CustomAuthenticationHandler(String name, ServicesManager servicesManager, PrincipalFactory principalFactory, Integer order) {
        super(name, servicesManager, principalFactory, order);
    }

    @Override
    protected HandlerResult authenticateUsernamePasswordInternal(UsernamePasswordCredential usernamePasswordCredential, String originalPassword) {
        logger.info("Login User:{}.", usernamePasswordCredential.getUsername());
        Map<String, Object> attributes = new HashMap<>();
        //通过参数控制能访问哪些应用
        attributes.put("APP_CODE_PORTAL", "ACCESS");
        Principal principal = principalFactory.createPrincipal(usernamePasswordCredential.getUsername(), attributes);
        return createHandlerResult(usernamePasswordCredential, principal, null);
    }
}
